package cn.djb.qz2021;

public class Main_ {
    public static void main(String[] args) {
        System.out.println(new Solution().maxSum("010010"));
    }

    private static class Solution {
        public int maxSum(String s) {
            int answer = 0;
            // i,0/1,index/sum;
            int[][][] sum = new int[s.length()][2][2];

            if (s.charAt(0) == '0') {
                sum[0][0] = new int[]{1, 1};
                sum[0][1] = new int[]{0, 0};
            } else {
                sum[0][0] = new int[]{0, 0};
                sum[0][1] = new int[]{1, 1};
            }
            answer = max(answer, sum[0][0][1], sum[0][1][1]);
            for (int i = 1; i < s.length(); i++) {
                if (s.charAt(i) == '0') {
                    sum[i][0][0] = sum[i - 1][0][0] + 1;
                    sum[i][0][1] = sum[i - 1][0][1] + sum[i][0][0];

                    sum[i][1][0] = sum[i - 1][1][0];
                    sum[i][1][1] = sum[i - 1][1][1];
                    answer = max(answer, sum[i][0][1], sum[i - 1][1][1] + 1);
                } else {
                    sum[i][0][0] = sum[i - 1][0][0];
                    sum[i][0][1] = sum[i - 1][0][1];

                    sum[i][1][0] = sum[i - 1][1][0] + 1;
                    sum[i][1][1] = sum[i - 1][1][1] + sum[i][1][0];
                    answer = max(answer, sum[i - 1][0][1] + 1, sum[i][1][1]);
                }
            }
            return answer;
        }

        int max(int a, int b, int c) {
            if (a < b) {
                a = b;
            }
            if (a < c) {
                a = c;
            }
            return a;
        }
    }
}